package httpCall

import (
	"encoding/json"
	"errors"
	"net/http"
)

//go数据类型转[]byte
func SetResponseJson(m interface{}) ([]byte, error) {
	return json.Marshal(m)
}

//设置Response的返回信息
func SetResponse(w *http.ResponseWriter, statusCode int, contentType string, data interface{}) error {
	if w == nil {
		return errors.New("http.ResponseWriter is nil")
	}
	if http.StatusText(statusCode) == "" {
		return errors.New("statusCode ")
	}
	if contentType == "" {
		contentType = "application/json"
	}
	(*w).WriteHeader(statusCode)
	(*w).Header().Set("content-type", contentType)
	msg, err := json.Marshal(data)
	if err != nil {
		return err
	}
	(*w).Write(msg)
	return nil
}
